package com.wxyzz.cem.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wxyzz.cem.enums.ThreadLocalName;
import com.wxyzz.cem.interceptor.UserInterceptor;
import com.wxyzz.cem.pojo.PaymentRecord;
import com.wxyzz.cem.pojo.User;
import com.wxyzz.cem.service.PaymentRecordService;
import com.wxyzz.cem.service.UserService;
import com.wxyzz.cem.util.Result;
import com.wxyzz.cem.util.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 缴费记录 前端控制器
 * </p>
 *
 * @author xc
 * @since 2024-07-04
 */
@RestController
@RequestMapping("/payment-record")
public class PaymentRecordController {
    @Autowired
    private PaymentRecordService paymentRecordService;
    @Autowired
    private UserService userService;
    @GetMapping("/classId")
    @UserInterceptor.UserIntercept
    public Result<Page<PaymentRecord>> getPaymentRecordByClassId(@RequestParam(value = "page", defaultValue = "1" ,required = false) Integer page,
                                                     @RequestParam(value = "size", defaultValue = "10",required = false) Integer size) {
        //        从token中获取用户id
        Integer userId = ThreadLocalUtil.get(ThreadLocalName.USER.getValue(), User.class).getId();
        User user = userService.getById(userId);
        Integer classId = user.getClassId();
        Page<PaymentRecord> paymentRecordPage = paymentRecordService.getPaymentRecordByClassId(page, size, classId);
        return Result.success(paymentRecordPage);
    }

    @GetMapping("/userId")
    @UserInterceptor.UserIntercept
    public Result<Page<PaymentRecord>> getPaymentRecordByPage(@RequestParam(value = "page", defaultValue = "1" ,required = false) Integer page,
                                                              @RequestParam(value = "size", defaultValue = "10",required = false) Integer size) {
        Integer userId = ThreadLocalUtil.get(ThreadLocalName.USER.getValue(), User.class).getId();
        Page<PaymentRecord> paymentRecordPage = paymentRecordService.getPaymentRecordByUserId(page, size, userId);
        return Result.success(paymentRecordPage);
   }

    @GetMapping("/isPaid")
    @UserInterceptor.UserIntercept
    public Result<Boolean> isPaid(@RequestParam Integer paymentProjectId){
        Integer userId = ThreadLocalUtil.get(ThreadLocalName.USER.getValue(), User.class).getId();
        return Result.success(paymentRecordService.isPaid(paymentProjectId,userId));
    }
}
